% close all; clear; clc;
% 
% %tasks_num=24; agents_num=3;
% load A_con.mat
% load data30.mat
function [flag,obj1,obj2,t_score,total_path_time]=CBAA_Comunication_bundle_auction(A_type,agentX,taskX,taskV,capacy)

A=A_type;
% taskX=randi(500,24,2);
% taskV=rand(24,2);
%agentX=randi(500,3,2);
%save('data1.mat','taskX','taskV','agentX');
%agentX=[agentX;[348,124]];
%agents_num=size(A,1);
% agentX=randi(500,[agents_num,2]);
tasks_num=length(taskX); agents_num=length(agentX);

for i=1:tasks_num
    %编号，类型，位置，漂移速度，时间满意度参数（两参数）
    tasks(i) = Task(i, 1, taskX(i,:)', taskV(i,:)', 10, 0.01);
end

NuLt = 0;

% tasks(1).x = [11 11]';
% tasks(2).id = 20;


%编号，最大容量，任务类型，最大航行速度，初始位置,协调时间列数
for i=1:agents_num
    agents(i) = Agent_CBAA(i, capacy, [1 2], 6, agentX(i,:)', agents_num);
end
%A = zeros(length(agents));

for i=1:length(agents)
    agents(i).add_task(tasks);
    NuLt = NuLt + agents(i).Lt;
    
    % Adjacency Matrix Generation
    for k=(i+1):length(agents)
        agents(i).s(k) = 0;
        agents(k).s(i) = 0;
%         if (norm(agents(k).x - agents(i).x) <= 1500)
%             A(i,k) = 1;
%             A(k,i) = 1;
%         else
%             A(i,k) = 0;
%             A(k,i) = 0;
%         end
    end
end
%  A(1,2)=1;A(2,3)=1;A(3,4)=1;A(1,4)=1;
%  A(2,1)=1;A(3,2)=1;A(4,3)=1;A(4,1)=1;


tic;
dt = 0.1;
Tb = 50;

for t=0:dt:Tb

    %fprintf('T = %.1f \t=======================================>>\n', t);
    for i=1:length(agents)
        nmin = min(length(tasks),NuLt);        
        agents(i).bundle(0, nmin);
        for k=1:length(agents)
            if (A(i,k) == 1)
                agents(i).auction(agents(k).id, agents(k).y, agents(k).z, agents(k).s, t);
                %agents(k).auction(agents(i).id, agents(i).y, agents(i).z, agents(i).s, t);
            end
        end
                
        %fprintf('Agent-%d:', i);
        for j=1:length(agents(i).p)
            %fprintf('\t%d', agents(i).p(j));
        end
        %fprintf('\n');
    end
    
    %fprintf('\n');
    %pause(dt);
end
toc;
all_score=zeros(1,tasks_num);
for i=1:length(agents)
    agents(i).sp = 0;tau=0;
    pjn = [];
    for m = 1:length(agents(i).p)
        pjn = [pjn agents(i).p(m)];
        %Task.find_by_id(this.tasks, this.p(m))
        task_m = Task.find_by_id(agents(i).tasks, agents(i).p(m));
        all_score(agents(i).p(m))=max(task_m.score(tau + agents(i).tj(pjn)),task_m.score(tau + agents(i).tj(pjn)));
        agents(i).sp = agents(i).sp + task_m.score(tau + agents(i).tj(pjn));
    end
    %[val(i),val_path(i)] = agents(i).t_p(this, pn); 
end

%PlotAssignments(agents,tasks)
total_path_time=0;total_path_long=0;t_score=0;
costime=[];

for i=1:length(agents)
    %all_score(agents(i).p)=max(agents(i).sp, all_score(agents(i).p));
    if ~isempty(agents(i).p)
        costime=[costime,agents(i).times(end)+agents(i).dur_time];
        total_path_time=total_path_time+agents(i).times(end)+agents(i).dur_time;
        total_path_long=total_path_long+agents(i).t_p(agents(i).p);
    end
end
t_score=sum(all_score);

fprintf('\t 总满意度: %d', t_score);
fprintf('\n');
fprintf('\t 各艇用时总和: %d', total_path_time);
fprintf('\n');
fprintf('\t 任务完成时间: %d', min(costime));
fprintf('\n');
fprintf('\t 总路径: %d', total_path_long);
fprintf('\n');


tasks_b=[];
for i=1:length(agents)
    tasks_b=[tasks_b,agents(i).b];
end
if length(unique(tasks_b))<length(tasks_b)
    flag=0;
    disp('分配任务失败');
    obj1=length(tasks_b)-length(unique(tasks_b));
    disp(['冲突任务数：',num2str(obj1)]);
    obj2=tasks_num-length(unique(tasks_b));
    disp(['未分配任务数：',num2str(obj2)]);
else
    flag=1;
    disp('分配任务成功');
    obj1=0;
    obj2=0;
end
